package com.example.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entry.Product;
import com.example.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/*
 * 商品后台管理控制器
 * */
@RequestMapping("/admin/product")
@RestController
public class ProductController {

    @Autowired
    private IProductService productService;

    /*
     * 根据id查询信息
     * */
    @GetMapping("/product-id={id}")
    public Result getProductOne(@PathVariable Integer id){
        Product byId = productService.getById(id);
        return Result.success(byId);
    }

    /*
     * 根据id进行删除
     * */
    @GetMapping("/delete/id={id}")
    public Result deleteProductInfo(@PathVariable Integer id) {
        productService.removeById(id);
        return Result.success("删除成功");
    }

    /*
    * 批量删除
    * */
    @PostMapping("/batchd_delete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        productService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }


    @PostMapping("/import")
    public Result imp(@RequestParam("file") MultipartFile file) throws Exception {

        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        //表头必须是英文
        List<Product> products = reader.readAll(Product.class);
        for (int i = 0; i< products.size(); i++){
            products.get(i).setId(null);
        }

        productService.saveBatch(products);
        return Result.success("导入成功");
    }

    /*
     * 修改或保存信息
     * */
    @PostMapping("/update")
    public Result updateOrSavProduct(@RequestBody Product product){
        productService.saveOrUpdate(product);
        return Result.success("更新成功");
    }

    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public Result getPageProduct(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "1") Integer status,
                               @RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "") String address){

        System.out.println("status==================="+status);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        // 根据id进行降序排序
        queryWrapper.orderByDesc("id");
        // 查询管理员和普通用户
        if(!"".equals(status)){
            queryWrapper.like("status", status);
        }
        // 根据条件进行模糊查询
        if(!"".equals(name)){
            queryWrapper.like("name", name);
        }
        if(!"".equals(address)){
            queryWrapper.like("address", address);
        }

        // 分页构造器
        Page<Product> page = productService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

}
